package com.ruoyi.file.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.file.domain.TFileInfoVO;
import com.ruoyi.file.service.IFileChunkInfoService;
import com.ruoyi.framework.minio.MinioFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.file.mapper.FileInfoMapper;
import com.ruoyi.file.domain.FileInfo;
import com.ruoyi.file.service.IFileInfoService;

/**
 * 文件信息Service业务层处理
 *
 * @author rww
 * @date 2023-02-20
 */
@Service
public class FileInfoServiceImpl implements IFileInfoService
{
    @Autowired
    private FileInfoMapper fileInfoMapper;

    @Autowired
    private IFileChunkInfoService fileChunkInfoService;

    /**
     * 查询文件信息
     *
     * @param id 文件信息主键
     * @return 文件信息
     */
    @Override
    public FileInfo selectFileInfoById(Long id)
    {
        return fileInfoMapper.selectFileInfoById(id);
    }

    /**
     * 查询文件信息列表
     *
     * @param fileInfo 文件信息
     * @return 文件信息
     */
    @Override
    public List<FileInfo> selectFileInfoList(FileInfo fileInfo)
    {
        return fileInfoMapper.selectFileInfoList(fileInfo);
    }

    /**
     * 新增文件信息
     *
     * @param fileInfo 文件信息
     * @return 结果
     */
    @Override
    public int insertFileInfo(FileInfo fileInfo)
    {
        fileInfo.setId(IdUtil.getSnowflakeNextId());
        fileInfo.setCreateTime(DateUtils.getNowDate());
        return fileInfoMapper.insertFileInfo(fileInfo);
    }

    /**
     * 修改文件信息
     *
     * @param fileInfo 文件信息
     * @return 结果
     */
    @Override
    public int updateFileInfo(FileInfo fileInfo)
    {
        fileInfo.setUpdateTime(DateUtils.getNowDate());
        return fileInfoMapper.updateFileInfo(fileInfo);
    }

    /**
     * 批量删除文件信息
     *
     * @param ids 需要删除的文件信息主键
     * @return 结果
     */
    @Override
    public int deleteFileInfoByIds(Long[] ids)
    {
        return fileInfoMapper.deleteFileInfoByIds(ids);
    }

    /**
     * 删除文件信息信息
     *
     * @param id 文件信息主键
     * @return 结果
     */
    @Override
    public int deleteFileInfoById(Long id)
    {
        return fileInfoMapper.deleteFileInfoById(id);
    }

    @Override
    public FileInfo selectFileInfoByIdentifier(String identifier) {
        return fileInfoMapper.selectFileInfoByIdentifier(identifier);
    }

    @Override
    public FileInfo saveUploadFile(TFileInfoVO fileInfoVO, MinioFile upload) {
        //建议fileInfoVO 携带一个业务id
        FileInfo fileInfo= new FileInfo();
        fileInfo.setId(IdUtil.getSnowflakeNextId());
        fileInfo.setFileName(fileInfoVO.getName());
        fileInfo.setFileExt(FileUtils.getExt(fileInfoVO.getName()));
        fileInfo.setFileSize(fileInfoVO.getSize());
        fileInfo.setBucket(upload.getBucket());
        fileInfo.setObjectName(upload.getObjectName());
        fileInfo.setUrl(upload.getUrl());
        fileInfo.setIdentifier(fileInfoVO.getUniqueIdentifier());
//        fileInfo.setUploadBy();
        fileInfo.setUploadTime(new Date());
        insertFileInfo(fileInfo);
        fileChunkInfoService.deleteFileChunkInfoByIdentifier(fileInfoVO.getUniqueIdentifier());
        return fileInfo;
    }
}
